<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>合可被 K 整除的子数组</title>
</head>
<body>
  <script>
    function fn(nums, k) {
      let len = nums.length
      let befores = [0]
      for (let i = 0; i < len; i++) {
        befores.push(befores[i] + nums[i])
      }
      let count = 0
      let map = new Map()
      map.set(0, 1)
      for (let i = 1; i <= len; i++) {
        let mod = ((befores[i] % k) + k) % k
        if (map.has(mod)) {
          count += map.get(mod)
        }
        let c = map.has(mod) ? map.get(mod) : 0
        map.set(mod, c + 1)
      }
      return count
    }
    console.log(fn([4, 5, 0, -2, -3, 1], 5))
  </script>
</body>
</html>